#!/bin/bash
# 本脚本会帮您安装mysql、mycli，并修改mysql初始密码为Abc.12345
# 如果要使用在线安装mysql，则取消第1步的注释，并注释第2步
# 如果要使用本地安装mysql，则需要/usr/local/src有且只有对应版本mysql的tar包

# 1.部署yum仓库，在线安装mysql（在线安装有时会很慢）
#wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
#rpm -ivh mysql80-community-release-el7-3.noarch.rpm
#sed -i '21s/0/1/' /etc/yum.repos.d/mysql-community.repo
#sed -i '28s/1/0/' /etc/yum.repos.d/mysql-community.repo
#yum -y install mysql-community-server.x86_64

# 2.本地安装mysql 5.7 （确保tar包已上传到当前目录）
cd /usr/local/src
tar -xf mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar 
yum -y localinstall *.rpm

# 3.启动mysql
systemctl restart mysqld
systemctl enable mysqld

# 4.安装mycli
mkdir ~/.pip/
cat >> ~/.pip/pip.conf  <<-EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
EOF

yum install -y python-pip python-devel libffi-devel openssl-devel
pip install --upgrade pip
pip install --ignore-installed mycli==1.10.0
pip install pymysql==0.9.2

# 查看状态
ss -ant | grep 3306
mysql --version

# 修改mysql密码为：Abc.12345
# 交互式
#yum install -y expect					
#initial_pass=`grep password /var/log/mysqld.log | awk '{print $NF}'`
#set timeout 10 
#/usr/bin/expect <<-EOF
#spawn mysqladmin -uroot -p password "Abc.12345"
#expect {
#      "Enter password: "	{send "$initial_pass\r"}
#}
#expect eof
#EOF

# 非交互式修改密码
initial_pass=`grep password /var/log/mysqld.log | awk '{print $NF}'`
mysqladmin -uroot -p"$initial_pass" password "Abc.12345"
echo -e "\033[31m请使用'mycli -uroot -pAbc.12345'命令进入mysql。\033[0m"